﻿using Data.Contexto;
using Domain.Entidades;
using Domain.Interfaces.Repositorios;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;

namespace Data.Repositorios
{
    public class SolicitacaoRepository : RepositoryBase<solicitacoes_impressoras>, ISolicitacaoRepository
    {
        protected credisiteContext Db = new credisiteContext();
        protected credigeralContext Dbseg = new credigeralContext();


        public IEnumerable<VW_solicitacoes_impressoras> Listar(DateTime dt1, DateTime dt2, VW_usuarios usuario,Boolean flag)
        {
            try
            {


                IEnumerable<VW_solicitacoes_impressoras> sol = Db.VW_solicitacoes_impressoras.Where(x => x.sol_data <= dt2 && x.sol_data >= dt1);

                if (flag == false)
                { 
                sol = sol.Where(x => x.uni_codigo == usuario.uni_codigo || x.usu_codigo == usuario.usu_codigo);
                }

                sol = sol.OrderBy(x => x.uni_codigo);
                sol = sol.OrderByDescending(x => x.sol_data).ThenByDescending(x => x.sol_codigo).ToList();
                return sol;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Inserir(solicitacoes_impressoras sol, int user_codigo)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    sol.sol_usuario = user_codigo;
                    sol.sol_data = DateTime.Now;
                    sol.sol_horario = DateTime.Now.ToString("t");

                    Db.solicitacoes_impressoras.Add(sol);
                    Db.SaveChanges();


                    log logs = new log();
                    logs.log_modulo = 3;
                    logs.log_tipo = 2215;
                    logs.log_descricao = "Solicitação codigo: " + sol.sol_codigo;
                    logs.log_usuario = user_codigo;
                    logs.log_data = DateTime.Now;
                    logs.log_horario = DateTime.Now.ToString("T");

                    Dbseg.log.Add(logs);
                    Dbseg.SaveChanges();
                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public void deletar(solicitacoes_impressoras sol, int user_codigo)

        {
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {

                    Db.solicitacoes_impressoras.Remove(sol);
                    Db.SaveChanges();

                    log logs = new log();
                    logs.log_modulo = 3;
                    logs.log_tipo = 2216;
                    logs.log_descricao = "Solicitação codigo: " + sol.sol_codigo;
                    logs.log_usuario = user_codigo;
                    logs.log_data = DateTime.Now;
                    logs.log_horario = DateTime.Now.ToString("T");

                    Dbseg.log.Add(logs);
                    Dbseg.SaveChanges();
                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IEnumerable<tipo_solicitacao> carrega_tipo_solicitacoes()

        {
            try
            {
                return Db.tipo_solicitacao.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string carrega_impressora(int unidade, int setor)

        {
            try
            {

                var listaimpressoras = from t in Db.impressoras.OrderBy(x => x.imp_descricao) where t.imp_setor == setor && t.imp_unidade == unidade select new { id = t.imp_codigo, desc = t.imp_descricao};
                var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                string sJSON = oSerializer.Serialize(listaimpressoras.ToList());
                return sJSON;

      
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string carrega_nserie(int impressora)

        {
            try
            {

                var listaimpressoras = from t in Db.impressoras.OrderBy(x => x.imp_descricao) where t.imp_codigo == impressora select new {  desc = t.imp_numero_serie };
                var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                string sJSON = oSerializer.Serialize(listaimpressoras.ToList());
                return sJSON;
              
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

 
        public VW_solicitacoes_impressoras carrega_sol_email(Int64 codigo)

        {

            try
            {

                return Db.VW_solicitacoes_impressoras.Where(x => x.sol_codigo == codigo).FirstOrDefault();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public solicitacoes_impressoras GetId(Int64 codigo)
        {

            return Db.solicitacoes_impressoras.Where(x => x.sol_codigo == codigo).FirstOrDefault();

        }

        public void reenviar_solicitacao(VW_solicitacoes_impressoras sol, int user_codigo)
        {
            log logs = new log();
            logs.log_modulo = 3;
            logs.log_tipo = 2216;
            logs.log_descricao = "Solicitação codigo: " + sol.sol_codigo;
            logs.log_usuario = user_codigo;
            logs.log_data = DateTime.Now;
            logs.log_horario = DateTime.Now.ToString("T");

            Dbseg.log.Add(logs);
            Dbseg.SaveChanges();

        }




    }
}